package com.zhiwei.codesecurity.web.handler;

import com.zhiwei.codesecurity.common.base.NullObjectResp;
import com.zhiwei.codesecurity.common.base.ServiceResponse;
import com.zhiwei.codesecurity.service.users.IUserService;
import com.zhiwei.codesecurity.service.users.request.CheckLoginReq;
import com.zhiwei.codesecurity.service.users.request.LoginOutReq;
import com.zhiwei.codesecurity.web.common.base.BaseRequest;
import com.zhiwei.codesecurity.web.common.base.RequestHead;
import com.zhiwei.codesecurity.web.common.utils.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("userCenterHandler")
public class UserCenterHandler {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IUserService userService;

    public ServiceResponse<Boolean> isLogin(RequestHead head) {
        CheckLoginReq model = new CheckLoginReq();
        model.setMemberNo(head.getUserId());
        model.setTokenId(WebUtil.getUserToken(head.getAccessToken()));
        ServiceResponse<Boolean> response = userService.doIsLogin(model);
        return response;
    }

    public ServiceResponse<NullObjectResp> doLogout(RequestHead head, BaseRequest baseReq) {
        LoginOutReq model = new LoginOutReq();
        model.setMemberNo(baseReq.getUserId());
        model.setTokenId(WebUtil.getUserToken(head.getAccessToken()));
        model.setSourceType(head.getOsType());
        ServiceResponse<NullObjectResp> response = userService.doLogout(model);
        return response;
    }
}
